import { _decorator, Component, Node, UITransform, Sprite, Animation, SpriteFrame, AnimationClip, animation } from 'cc'
const { ccclass, property } = _decorator

import { EntityManager } from '../../Base/EntityManager'

// 状态
import State from '../../Base/State'

// 状态机类型
import { PLAYER_STATE_TYPE_ENUM } from '../../Enums'

// 状态机
import { StateMchine, getPlayerState, getPlayerDirection } from '../../Base/StateMchine'

// 子状态机
import { IdleStateMchine } from './IdleStateMchine'
import { DeathStateMchine } from './DeathStateMchine'

export type pamersValueType = number | string | boolean

@ccclass('DoorStateMchine')
export class DoorStateMchine extends StateMchine {
  async init() {
    this.animationComponent = this.node.addComponent(Animation)

    this.initPramers() //初始化参数
    this.initStateMachines() //初始化状态机
    this.initAnimationEvent()

    await Promise.all(this.waitingList)
  }

  // 初始化参数
  initPramers() {
    this.pramers.set(PLAYER_STATE_TYPE_ENUM.IDLE, getPlayerState())
    this.pramers.set(PLAYER_STATE_TYPE_ENUM.DIRECTION, getPlayerDirection())
    this.pramers.set(PLAYER_STATE_TYPE_ENUM.DEATH, getPlayerState())
  }

  // 初始化状态机
  initStateMachines() {
    this.stateMachines.set(PLAYER_STATE_TYPE_ENUM.IDLE, new IdleStateMchine(this))
    this.stateMachines.set(PLAYER_STATE_TYPE_ENUM.DEATH, new DeathStateMchine(this))
  }

  initAnimationEvent() {}

  //  定义transition
  run() {
    switch (this.currentState) {
      case this.stateMachines.get(PLAYER_STATE_TYPE_ENUM.IDLE):
      case this.stateMachines.get(PLAYER_STATE_TYPE_ENUM.DEATH):
        if (this.getPramers(PLAYER_STATE_TYPE_ENUM.DEATH)) {
          this.currentState = this.stateMachines.get(PLAYER_STATE_TYPE_ENUM.DEATH) as State
        } else if (this.getPramers(PLAYER_STATE_TYPE_ENUM.IDLE)) {
          this.currentState = this.stateMachines.get(PLAYER_STATE_TYPE_ENUM.IDLE) as State
        } else {
          this.currentState = this.currentState as State
        }
        break
      default:
        this.currentState = this.stateMachines.get(PLAYER_STATE_TYPE_ENUM.IDLE) as State
    }
  }
}
